import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterstudyapplast/common/event/events.dart';
import 'package:flutterstudyapplast/manager/app_manager.dart';
import 'package:flutterstudyapplast/services/api/api.dart';
import 'package:flutterstudyapplast/services/api/api_implementation.dart';
import 'package:flutterstudyapplast/ui/page/page_collect.dart';
import 'package:flutterstudyapplast/ui/page/page_login.dart';

class MainDrawer extends StatefulWidget {
  @override
  _MainDrawerState createState() => _MainDrawerState();
}

class _MainDrawerState extends State<MainDrawer> {

  String username;

  @override
  void initState() {
 // TODO: implement initState
    super.initState();
    AppManager.eventBus.on<LoginEvent>().listen((event){
      username = event.username;
      AppManager.prefs.setString(AppManager.ACCOUNT, event.username);
      setState(() {});
    });
    username = AppManager.prefs.getString(AppManager.ACCOUNT);
  }

  @override
  Widget build(BuildContext context) {
    Widget userHeader = DrawerHeader(
      decoration: BoxDecoration(color: Colors.blue),
      child: InkWell(
        onTap: (){
          ///未登录进入登录页
          if(username != null){
            Navigator.push(context, MaterialPageRoute(builder: (cotenxt){
              return CollectPage();
            }));
          }else{
            Navigator.push(context, MaterialPageRoute(builder: (cotenxt){
              return LoginPage();
            }));
          }

        },
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(bottom: 10),
              child: CircleAvatar(
                backgroundImage: AssetImage("assets/images/logo.png"),
                radius: 30,
              ),
            ),
            Text(username??"请先登录",style: TextStyle(fontSize: 18,color: Colors.white))
          ],
        ),
      ),
    );
    return Container(
      color: Colors.white,
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          userHeader,
          InkWell(
            onTap: (){
              ///未登录进入登录页，登录后进入收藏
              if(AppManager.isLogin()){
                Navigator.push(context, MaterialPageRoute(builder: (cotenxt){
                  return CollectPage();
                }));
              }else{
                Navigator.push(context, MaterialPageRoute(builder: (cotenxt){
                  return LoginPage();
                }));
              }

            },
            child: ListTile(leading: Icon(Icons.favorite),title: Text("收藏列表",style: TextStyle(fontSize: 16),),),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(18.0, 0.0, 18.0, 0.0),
            child: Divider(color: Colors.grey,),
          ),
          Offstage(
            offstage: username == null,
            child: InkWell(
              onTap: (){
                ///点击退出登录
                setState(() {
                  AppManager.prefs.setString(AppManager.ACCOUNT, null);
                  ApiImpl.clearCookie();
                  username = null;
                  AppManager.eventBus.fire(LoginOutEvent());
                });
              },
              child: ListTile(leading: Icon(Icons.exit_to_app),title: Text("退出登录",style: TextStyle(fontSize: 16),),),
            ),
          )
        ],
      ),

    );
  }
}
